

#include "application.h"
#include "ddl.h"
#include "hal_adc.h"
#include "mode_button.h"
#include "valve.h"

/* 全局变量 */
controller_t   controller;
blow_back_t    blow_back;
unsigned short power_up_count              = 0;
unsigned short power_down_count            = 0;
unsigned short mode_button_release_count   = 0;
unsigned short mode_button_press_count     = 0;
unsigned short driver_button_press_count   = 0;
unsigned short driver_button_release_count = 0;

static driver_signal_t driver_signal_tmp;

/**
 * @brief	自动反吹函数
 */
static void blow_back_manual(void)
{
    blow_back.manual_blow_command = true;
}

/**
 * @brief	自动反吹函数
 * @note	本函数只改变标志位，实际动作在 timer1 中断服务函数中执行
 */
static void blow_back_auto(void)
{
    blow_back.auto_blow_command = true;
}

/* 更新母线电压 */
static void update_ubus(void)
{
    controller.ubus = 10 * convert_data_to_voltage(adc_data[0]);
}

/* 更新钥匙电（K15）信号 */
static void uptate_k15(void)
{
    if ((power_up_count > 1000) && (power_down_count == 0))
    {
        controller.k15_signal = power_on;
    }
    else if ((power_up_count == 0) && (power_down_count > 1000))
    {
        controller.k15_signal = power_off;
    }
    else
    {
        controller.k15_signal = error;
    }
}

/* 更新模式按钮 */
static void uptate_mode_button(void)
{
    if (mode_button_press_count > 500)
    {
        controller.blow_mode = manual_mode;
        turn_off_mode_button_led();
    }
    else if (mode_button_release_count > 500)
    {
        controller.blow_mode = auto_mode;
        turn_on_mode_button_led();
    }
}

/* 司机反吹按钮控制 */
static void update_driver_signal(void)
{
    if (driver_button_press_count >= 100)
    {
        driver_signal_tmp = driver_need_blow;
    }

    if (driver_button_release_count > 100)
    {
        controller.driver_signal = driver_signal_tmp;
        driver_signal_tmp        = driver_not_blow;
    }
    else
    {
        controller.driver_signal = driver_not_blow;
    }
}

/* 更新系统数据 */
void update_data(void)
{
    update_ubus();
    uptate_k15();
    uptate_mode_button();
    update_driver_signal();
}

/**
 * @brief	反吹变量初始化
 */
void blow_back_init(void)
{
    blow_back.pick_up_flag        = false;
    blow_back.auto_blow_command   = false;
    blow_back.manual_blow_command = false;
    blow_back.blow_delay_time     = 2 * 10;  // 2s
    blow_back.blow_interval_time  = 15 * 10; // 15s
    blow_back.blow_delay_count    = 0;
    blow_back.blow_interval_count = 0;
    blow_back.process             = none;
}

/**
 * @brief	控制器变量初始化
 */
void controller_init(void)
{
    controller.ubus        = 0.0f;
    controller.valve_cur   = 0.0f;
    controller.heater_cur  = 0.0f;
    controller.temperature = 0.0f;

    controller.blow_mode     = auto_mode;
    controller.driver_signal = driver_not_blow;
    controller.k15_signal    = power_off;
}

/**
 * @brief   自动反吹过程执行函数，须在中断周期为100ms的中断函数中调用
 */
void auto_blow_back_process(void)
{
    switch (blow_back.process)
    {
    case blow_delay:
        if (blow_back.blow_delay_count >= blow_back.blow_delay_time)
        {
            blow_back.process = first_blow;
        }
        else
        {
            blow_back.blow_delay_count++;
        }
        break;

    case first_blow:
        blow_back.blow_delay_count = 0;
        open_valve();
        blow_back.process             = first_interval;
        blow_back.blow_interval_count = 0;
        break;

    case first_interval:
        close_valve();
        blow_back.blow_interval_count++;
        if (blow_back.blow_interval_count >= blow_back.blow_interval_time)
        {
            blow_back.process = second_blow;
        }
        break;

    case second_blow:
        open_valve();
        blow_back.process             = second_interval;
        blow_back.blow_interval_count = 0;
        break;

    case second_interval:
        close_valve();
        blow_back.blow_interval_count++;
        if (blow_back.blow_interval_count >= blow_back.blow_interval_time)
        {
            blow_back.process = third_blow;
        }
        break;

    case third_blow:
        open_valve();
        blow_back.process = end;
        break;

    case end:
        close_valve();
        blow_back.auto_blow_command = false;
        blow_back.process           = none;

    case none:
        break;

    default:
        break;
    }
}

/**
 * @brief	手动反吹函数执行过程,需在100ms 中断服务函数中直接调用
 */
void manual_blow_back_process(void)
{
    if (blow_back.manual_blow_command == false)
    {
        close_valve();
        return;
    }
    open_valve();
    delay1ms(100);
    blow_back.manual_blow_command = false;
}

/* 反吹逻辑判断*/
void blow_back_logic(void)
{
    if (controller.blow_mode == auto_mode)
    {
        /* 检测钥匙电状态 */
        if (controller.k15_signal == power_on)
        {
            blow_back.pick_up_flag = true;
        }

        /* 下电之后反吹 */
        if (blow_back.pick_up_flag == true)
        {
            if (controller.k15_signal == power_off)
            {
                blow_back_auto();
                blow_back.pick_up_flag = false;
            }
        }
    }
    else if (controller.blow_mode == manual_mode)
    {
        if (controller.driver_signal == driver_need_blow)
        {
            blow_back_manual();
            controller.driver_signal = driver_not_blow;
        }
        else
        {
            controller.driver_signal = driver_not_blow;
        }
    }
}
